//main.cc
#include <iostream>
#include <unistd.h>
#include <string>
#include <pthread.h>
#include "Task.hpp"
#include "RingQueue.hpp"
using namespace std;
void *ProductorRun(void *arg) // 生产者线程要使用的函数
{
    RingQueue<Task> *rq = static_cast<RingQueue<Task> *>(arg);
    while (1)
    {
        // 创建Task任务
        char arr[] = "+-*/%";
        int x = rand() % 10;
        int y = rand() % 20;
        char c = arr[rand() % 5];
        Task t(x, y, c);
        rq->push(t);
        cout << t.to_productor() << endl;
    }

    return nullptr;
}

void *CustomerRun(void *arg) // 消费者线程要使用的函数
{
    RingQueue<Task> *rq = static_cast<RingQueue<Task> *>(arg);
    while (1)
    {
        sleep(3);
        Task t;
        rq->pop(&t);
        t();
        cout << t.to_customer() << endl;
    }

    return nullptr;
}

int main()
{
    srand((uint64_t)time(0));
    RingQueue<Task>* rq=new RingQueue<Task>;
    pthread_t customer[3];
    pthread_t productor[3];
    pthread_create(&customer[0],nullptr,CustomerRun,rq);
    pthread_create(&customer[1],nullptr,CustomerRun,rq);
    pthread_create(&customer[2],nullptr,CustomerRun,rq);
    pthread_create(&productor[0],nullptr,ProductorRun,rq);
    pthread_create(&productor[1],nullptr,ProductorRun,rq);
    pthread_create(&productor[2],nullptr,ProductorRun,rq);
    
    //join
    pthread_join(customer[0],nullptr);
    pthread_join(customer[1],nullptr);
    pthread_join(customer[2],nullptr);
    pthread_join(productor[0],nullptr);
    pthread_join(productor[1],nullptr);
    pthread_join(productor[2],nullptr);
    



    return 0;
}
